package com.pickgliss.ui.controls.container
{
	import com.pickgliss.events.ComponentEvent;
	import com.pickgliss.instance.IManualSize;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.utils.Directions;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.core.INavigatorContent;

	/**
	 * 
	 * @author Herman
	 * 纵向排列的容器对象
	 */	
	public class VBox extends BoxContainer
	{

		public function VBox()
		{
			super();
		}
		public var firstYPos:Number = 0;
		override protected function arrange():void
		{
			_width = 0;
			_height = 0;
			var ypos:Number = firstYPos;
			for(var i:int = 0; i < _childrenList.length; i++)
			{
				var child:DisplayObject = _childrenList[i];
				child.y = ypos;
				var childHeight:Number = getItemHeight(child);
				var childWidth:Number = getItemWidth(child);
				ypos += childHeight;
				ypos += _spacing;
				_height += childHeight;
				_width = Math.max(_width,childWidth);
				refreshDirection(child);
			}
			_height += _spacing * (numChildren - 1);
			_height = Math.max(0,_height);
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		override protected function refreshDirection(child:DisplayObject):void
		{
			if(_direction == Directions.DIRECTION_R && isStrictWidth)
			{
				child.x = _strictWidth - getItemWidth(child);
			}
		}
		override public function removeAllChild():void
		{
			var number:int = numChildren;
			for (var i:int = 0; i < number; i ++)
			{
				var child:DisplayObject = getChildAt(0);
				removeChild(child);
				child.y = 0;
				child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			}
			if(_childrenList)_childrenList.splice(0,-1);
		}
		private function getItemWidth(child:DisplayObject):Number
		{
			if(child is IManualSize)return (child as IManualSize).viewRectangle.width;
			if(child is Component)return (child as Component).displayWidth;
			return child.width;
		}
		private function getItemHeight(child:DisplayObject):Number
		{
			if(isStrictHeight)return _strictHeight;
			if(child is IManualSize)return (child as IManualSize).viewRectangle.height;
			if(child is Component)return (child as Component).displayHeight;
			return child.height;
		}
	}
}